package edu.hunau.cxb18.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 提供获得SqlSession的工具类
 * @author hb
 * @create 2020-02-24 11:53
 */
public class SqlSessionUtil {

    private static final SqlSessionFactory SESSION_FACTORY;

    static{
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SESSION_FACTORY = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 工具类，获得SqlSession
     * @return
     */
    public static SqlSession openSession(){
        return SESSION_FACTORY.openSession();
    }
}
